package com.hjt.tools.generation.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileIO {


	public static List<String> readFile(String path, List<Integer> flagList) {
		List<String> strList = new ArrayList<String>();
		try{
			FileReader fr = new FileReader(path);   
			BufferedReader br = new BufferedReader(fr);   
			int count = 0;
			String fileText = br.readLine();   
			//count ++;
			while (fileText != null) {   
				if(fileText.contains("@#@")){
					flagList.add(count);
				}
				strList.add(fileText);
				fileText = br.readLine();   
				count++;
			}   
			br.close();   
			fr.close();
		}catch(IOException e){
			System.out.println("读取文件失败:" + e.getMessage());
		}
		return strList;
	}
	
	public static void writeFile(List<String>text, String filePath) {
		try {
			File file=new File(filePath);
			if(!file.exists())
				file.createNewFile();
			FileOutputStream out=new FileOutputStream(file,false); //如果追加方式用true        
			StringBuffer sb=new StringBuffer();
			for(int i=0,len=text.size(); i<len; i++){
				sb.append(text.get(i)+"\n");
			}
			out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集
			out.close();
		}
		catch(IOException ex){
			System.out.println(ex.getStackTrace());
		}
	}
	
}
